package com.example.yy.fragementtest318yy.model;



import com.example.yy.fragementtest318yy.bean.ArticleBean;
import com.example.yy.fragementtest318yy.bean.TcaseBean;
import com.example.yy.fragementtest318yy.iface.ArticleListener;
import com.example.yy.fragementtest318yy.iface.TcaseListener;
import com.example.yy.fragementtest318yy.iface.Tcaseiface;
import com.example.yy.fragementtest318yy.service.ArticleService;
import com.example.yy.fragementtest318yy.service.TcaseService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by yy on 2017/4/20.
 */

public class TcaseModel implements Tcaseiface {

    private Retrofit retrofit;
    private String BASEURL =
            "http://amicool.neusoft.edu.cn/";

    public TcaseModel() {
        retrofit = new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
    }

    @Override
    public void getResultList(String mod, int page, String sessionID, final TcaseListener tcaseListener) {
        TcaseService tcaseService = retrofit.create(TcaseService.class);
        Call<List<TcaseBean>> call = tcaseService.getResult(mod,page,sessionID);
        call.enqueue(new Callback<List<TcaseBean>>() {
            @Override
            public void onResponse(Call<List<TcaseBean>> call, Response<List<TcaseBean>> response) {
                if(response.isSuccessful()&&response!=null){
                    tcaseListener.onResponse(response.body());
                }else
                    tcaseListener.onFail("fail");
            }

            @Override
            public void onFailure(Call<List<TcaseBean>> call, Throwable t) {
                tcaseListener.onFail(t.toString());
            }
        });
    }
}
